package org.zsk.cs.controller;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.zsk.cs.domain.vo.MsgRequest;
import org.zsk.cs.domain.vo.MsgResponse;
import org.zsk.cs.response.ResultBody;

/**
 * <p>
 *     websocket controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/4/17
 */
@RestController
public class WebSocketController {

    private final SimpMessagingTemplate messagingTemplate;

    public WebSocketController(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @MessageMapping("/center")
    public ResultBody forum(@RequestBody MsgRequest message) throws Exception {
        messagingTemplate.convertAndSend(message.getTo()
                ,new MsgResponse(message.getFrom()+": "+message.getMsg()));
        return ResultBody.success();
    }
}
